package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.annotation.AutoFill;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;

import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.enumeration.OperationType;
import com.sky.mapper.CategoryMapper;

import com.sky.result.PageResult;
import com.sky.service.ICategoryService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.sky.constant.StatusConstant.ENABLE;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-18
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    @AutoFill(value = OperationType.INSERT)//自动填充创建时间和修改时间等字段
    public void Mysave(CategoryDTO categoryDTO) {
        //将DTO转换为实体
        Category category = new Category();
        BeanUtil.copyProperties(categoryDTO, category);

        //设置创建人和修改人
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        //设置启动状态
        category.setStatus(ENABLE);//启用状态
        //保存实体
        this.save(category);
    }

    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //1.获取当前页码和每页显示的条数
        int page = categoryPageQueryDTO.getPage();
        int pageSize = categoryPageQueryDTO.getPageSize();
        //2.创建分页查询条件
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(categoryPageQueryDTO.getName() != null && !categoryPageQueryDTO.getName().isEmpty(), Category::getName, categoryPageQueryDTO.getName())
                .eq(categoryPageQueryDTO.getType() != null, Category::getType, categoryPageQueryDTO.getType());

        //排序
        wrapper.orderByDesc(Category::getCreateTime)
                .orderByAsc(Category::getSort);


        //3.创建分页对象
        Page<Category> p = new Page<>(page, pageSize);
        //4.执行分页查询
        this.page(p, wrapper);
        //5.封装返回结果
        return new PageResult(p.getTotal(), p.getRecords());
    }

    @AutoFill(value = OperationType.UPDATE)//自动填充修改时间等字段
    @Override
    public void Myupdate(CategoryDTO categoryDTO) {
        //将DTO转换为实体
        Category category = new Category();
        BeanUtil.copyProperties(categoryDTO, category);

        //设置修改人
        category.setUpdateUser(BaseContext.getCurrentId());

        //设置启动状态
        category.setStatus(ENABLE);//启用状态
        category.setUpdateTime(new Date());
        //创建更新条件
        LambdaUpdateWrapper<Category> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Category::getId, categoryDTO.getId())
                .set(category.getName() != null && !category.getName().isEmpty(), Category::getName, category.getName())
                .set(category.getSort() != null, Category::getSort, category.getSort())
                .set(category.getStatus() != null, Category::getStatus, category.getStatus())
                .set(category.getType() != null, Category::getType, category.getType())
                .set(category.getUpdateUser() != null, Category::getUpdateUser, category.getUpdateUser())
                .set(category.getUpdateTime() != null, Category::getUpdateTime, category.getUpdateTime());
        //执行更新
        this.update(wrapper);


    }

    @AutoFill(value = OperationType.UPDATE)//自动填充修改时间等字段
    @Override
    public void startOrStop(Long id, Integer status) {
        Category category = Category.builder()
                .id(id)
                .status(status)
                .updateUser(BaseContext.getCurrentId())
                .build();

        category.setUpdateTime(new Date());
        //创建更新条件
        LambdaUpdateWrapper<Category> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Category::getId, category.getId())
                .set(category.getName() != null && !category.getName().isEmpty(), Category::getName, category.getName())
                .set(category.getSort() != null, Category::getSort, category.getSort())
                .set(category.getStatus() != null, Category::getStatus, category.getStatus())
                .set(category.getType() != null, Category::getType, category.getType())
                .set(category.getUpdateUser() != null, Category::getUpdateUser, category.getUpdateUser())
                .set(category.getUpdateTime() != null, Category::getUpdateTime, category.getUpdateTime());
        //执行更新
        this.update(wrapper);

    }

    @Override
    public List listByType(Integer type) {
        //创建查询条件
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(type != null, Category::getType, type);
        //排序
        wrapper.orderByAsc(Category::getSort)
                .orderByDesc(Category::getUpdateTime);
        //执行查询
        return this.list(wrapper);
    }
}
